//
//  CompleteViewController.m
//  lhpc_manager
//
//  Created by 李英杰 on 2017/9/6.
//  Copyright © 2017年 zhaoyongjie. All rights reserved.
//

#import "CompleteViewController.h"
#import "OrderTableViewCell.h"
#import "LHNetworking.h"
#import <YYKit/YYKit.h>
#import <CYLTableViewPlaceHolder/CYLTableViewPlaceHolder.h>
#import "LHEmptyView.h"

@interface CompleteViewController ()<UITableViewDelegate,UITableViewDataSource,CYLTableViewPlaceHolderDelegate>

@property (nonatomic, strong) UITableView * tableView;

@property (nonatomic, assign) NSInteger page;

@property (nonatomic, strong) NSMutableArray * dataArray;


@end

@implementation CompleteViewController

- (UIView *)makePlaceHolderView {
    LHEmptyView *a = [[LHEmptyView alloc] init];
    a.descLabel.text = @"您还未提现过哟,快提现试试吧~";
    
    return a;
}

- (BOOL)enableScrollWhenPlaceHolderViewShowing {
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = BACKCOLOR;
    
    [self setMainView];
    
    [self.tableView.mj_header beginRefreshing];
    
}

-(void)getData{
    
    NSDictionary * parameters = @{@"page" : @(self.page), @"size" : @(10)};
    
    LHPC_WEAK_SELF;
    [[LHNetworking shared] POST:LHSetRequestAPI(LHHomeWithDrawedList) parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        LHPC_STRONG_SELF;
        
        NSLog(@"%@",responseObject);
        [strongSelf handleResultWithDic:responseObject];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        LHPC_STRONG_SELF;
        [strongSelf.tableView.mj_header endRefreshing];
        [strongSelf.tableView.mj_footer endRefreshing];
        [HLProgressHUD showHudTipStr:LHNetWorkErrorMessage];
    }];
}

- (void)handleResultWithDic:(NSDictionary *)response {
    NSArray *arr = response[@"data"][@"stroke"];
    self.tableView.mj_footer.hidden = !(arr.count > 0);
    if (self.page == 0) {
        [self.dataArray removeAllObjects];
    }
    
    if (self.page == 0) {
        [self.tableView.mj_header endRefreshing];
        if (arr.count < 10) {
            self.tableView.mj_footer.hidden = YES;
        }else{
            [self.tableView.mj_footer resetNoMoreData];
        }
        
    } else if (arr.count < 10) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } else {
        [self.tableView.mj_footer endRefreshing];
    }
    
    for (int i = 0; i < arr.count; i++) {
        OrderModel *model = [OrderModel modelWithJSON:arr[i]];
        [self.dataArray addObject:model];
    }
    
    [self.tableView cyl_reloadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 140;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    OrderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"completeordertableviewcellid" forIndexPath:indexPath];
    
    
    [cell setOrderValueWith:self.dataArray[indexPath.row]];
    
    return cell;
    
}

-(void)setMainView{
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
        
    }];
}




-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.separatorColor = LINECOLOR;
        _tableView.backgroundColor = BACKCOLOR;
        
        
        [_tableView registerClass:[OrderTableViewCell class] forCellReuseIdentifier:@"completeordertableviewcellid"];
        
        LHPC_WEAK_SELF;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            LHPC_STRONG_SELF;
            
            strongSelf.page = 0;
            strongSelf.tableView.mj_footer.state = MJRefreshStateIdle;
            [strongSelf getData];
            
        }];
        
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            LHPC_STRONG_SELF;
            weakSelf.page += 1;
            [strongSelf getData];
            
        }];
        
        _tableView.mj_footer.hidden = YES;
        
    }
    return _tableView;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
